package com.beiming.libra.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.beiming.libra.common.AppException;
import com.beiming.libra.common.Assert;
import com.beiming.libra.common.constants.LibraConst;
import com.beiming.libra.common.constants.ValidateMessage;
import com.beiming.libra.common.enums.ErrorCode;
import com.beiming.libra.common.enums.StatusEnum;
import com.beiming.libra.common.page.Page;
import com.beiming.libra.common.page.PageResult;
import com.beiming.libra.dao.AuthRoleRelationMapper;
import com.beiming.libra.dao.RoleMapper;
import com.beiming.libra.dao.UserRoleRelationMapper;
import com.beiming.libra.domain.AuthRoleRelation;
import com.beiming.libra.domain.Role;
import com.beiming.libra.domain.UserRoleRelation;
import com.beiming.libra.domain.dto.requestdto.RoleRequestDTO;
import com.beiming.libra.service.RoleService;

/***
 * 
 * @author weibo
 * */
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
  @Resource
  private RoleMapper roleMapper;
  @Resource
  private UserRoleRelationMapper userRoleRelationMapper;
  @Resource
  private AuthRoleRelationMapper authRoleRelationMapper;

  /**
   * * 新增角色
   */
  @Override
  public void insertRole(RoleRequestDTO roleRequestDTO) {
    String roleName = roleRequestDTO.getRoleName();
    Assert.isNotEmpty(roleName, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Role roleSelect = new Role();
    roleSelect.setRoleName(roleRequestDTO.getRoleName());
    int roleCount = roleMapper.selectCount(roleSelect);
    if (roleCount > 0) {
      throw new AppException(ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.NAME_EXISTS);
    }
    Role role = new Role();
    role.setRoleName(roleName);
    role.setStatus(StatusEnum.USED.getCode());
    role.setRemark(roleRequestDTO.getRemark());
    role.setCreateTime(new Date());
    role.setCreateUser(roleRequestDTO.getCreateUser());
    role.setVersion(LibraConst.DEFAULT_VERSION);
    roleMapper.insert(role);
  }

  /***
   * 获取角色信息
   */
  @Override
  public Role getRole(String roleId) {
    Assert.isNotNull(roleId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    return roleMapper.selectByPrimaryKey(roleId);
  }

  /***
   * 获取角色列表
   */
  @SuppressWarnings("unchecked")
  @Override
  public PageResult<Role> listRole(RoleRequestDTO roleRequestDTO) {
    List<Role> list =
        roleMapper.listRole(roleRequestDTO.getRoleName(), (roleRequestDTO.getPageNo() - 1)
            * roleRequestDTO.getPageSize(), roleRequestDTO.getPageSize());
    PageResult<Role> pageResult = new PageResult<Role>();
    pageResult.setList(list);
    int total = roleMapper.listRoleCount(roleRequestDTO.getRoleName());
    Page page = new Page();
    page.setPageNo(roleRequestDTO.getPageNo());
    page.setPageSize(roleRequestDTO.getPageSize());
    page.setTotalRecords(total);
    pageResult.setPage(page);
    return pageResult;
  }

  /***
   * 更新角色信息
   */
  @Override
  public void updateRole(RoleRequestDTO roleRequestDTO) {
    String roleId = roleRequestDTO.getId();
    Assert.isNotNull(roleId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Role role = roleMapper.selectByPrimaryKey(roleId);
    Assert.isNotNull(roleId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_EXISTS);
    String roleName = roleRequestDTO.getRoleName();
    if (!StringUtils.isEmpty(roleName)) {
      Role roleSelect = new Role();
      roleSelect.setRoleName(roleName);
      List<Role> roleList = roleMapper.select(roleSelect);
      if (roleList.size() > 1) {
        throw new AppException(ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.NAME_EXISTS);
      } else if (roleList.size() == 1 && roleList.get(0).getId().equals(roleId)) {
        throw new AppException(ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.NAME_EXISTS);
      }
      role.setRoleName(roleName);
    }
    if (!StringUtils.isEmpty(roleRequestDTO.getRemark())) {
      role.setRemark(roleRequestDTO.getRemark());
    }
    role.setUpdateTime(new Date());
    role.setUpdateUser(roleRequestDTO.getUpdateUser());
    roleMapper.updateByPrimaryKey(role);
  }

  /***
   * 删除角色
   */
  @Override
  @Transactional
  public void deleteRole(String roleId) {
    Assert.isNotNull(roleId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_BLANK);
    Role role = roleMapper.selectByPrimaryKey(roleId);
    Assert.isNotNull(roleId, ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ID_NOT_EXISTS);
    UserRoleRelation userRoleRelation = new UserRoleRelation();
    userRoleRelation.setRoleId(roleId);
    int userRoleCount = userRoleRelationMapper.selectCount(userRoleRelation);
    if (userRoleCount > 0) {
      throw new AppException(ErrorCode.ILLEGAL_PARAMETER, ValidateMessage.ROLE_BEING_USED);
    }
    // 删除角色和菜单的对应关系
    AuthRoleRelation authRoleRelation = new AuthRoleRelation();
    authRoleRelation.setRoleId(roleId);
    authRoleRelationMapper.delete(authRoleRelation);
    // 删除角色信息
    roleMapper.delete(role);
  }
}
